/*** 
 * Trigger Name: FulfillmentGroupTrigger
 * Author: Accenture
 * Date: 26-sep-2011
 * Requirement/Project Name: iFulfill
 * Requirement Description: This trigger will not allow user to 
 *                          create duplicate sequence with 'Active' status in the workflow Fulfillment Group object.
 **/
trigger FulfillmentGroupTrigger on Workflow_Fulfillment_Group__c (before insert,before update) {
    Map<string,Workflow_Fulfillment_Group__c > ComboMap = new Map<string,Workflow_Fulfillment_Group__c >();
    set<id> newIds = new set<id>();
    for(Workflow_Fulfillment_Group__c WFG : trigger.new){    
        ComboMap.put(WFG.UniqueCheck__c, WFG);
        newIds.add(WFG.id);
    }
    for (Workflow_Fulfillment_Group__c WFGrp : [SELECT id,status__c, UniqueCheck__c  FROM Workflow_Fulfillment_Group__c WHERE UniqueCheck__c IN :ComboMap.keySet() and Status__c = 'Active' and id not in :newIds])
    {
        Workflow_Fulfillment_Group__c newWFGrp  = ComboMap.get(WFGrp.UniqueCheck__c);
        if(newWFGrp !=null)
        {
            if(newWFGrp.status__c == 'Active') 
                newWFGrp.addError('Cannot add duplicates'); 
        }
    }
}